import { CODE, MyAjax, API_URL } from '../config';

export const get_deploying_tasks = 'get_deploying_tasks';
export const get_trash_tasks = 'get_trash_tasks';
export const get_lined_tasks = 'get_lined_tasks';
export const get_task_detail = 'get_task_detail';

export function function_get_deploy_task(page, dispatch, cb){
  MyAjax(API_URL.deployingTask, {
    page
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: get_deploying_tasks,
        list: result.data.list
      });
      cb(result.data.page, result.data.total);
    }
  })
}

export function function_close_task(projectId, taskId, cb){
  MyAjax(API_URL.closeTask, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      cb();
    }
  })
}

export function function_get_trash_task(projectId, page, dispatch, cb){
  MyAjax(API_URL.getTrashTask, {
    projectId,
    page
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: get_trash_tasks,
        list: result.data.list
      });
      cb(result.data.page, result.data.total);
    }
  })
}

export function function_get_lineed_task(page, dispatch, cb){
  MyAjax(API_URL.getLineTask, {
    page
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: get_lined_tasks,
        list: result.data.list
      });
      cb(result.data.page, result.data.total);
    }
  })
}

export function function_set_tester(projectId, taskId, tester, cb){
  MyAjax(API_URL.setTaskTester, {
    projectId,
    taskId,
    testerUid: tester
  }, function (result) {
    cb(result);
  })
}

export function function_set_admin(projectId, taskId, admin, cb){
  MyAjax(API_URL.setTaskAdmin, {
    projectId,
    taskId,
    reviewerUid: admin
  }, function (result) {
    cb(result);
  })
}

export function function_get_task(projectId, taskId, dispatch, cb){
  MyAjax(API_URL.getTask, {
    projectId,
    taskId
  }, function (result) {
    if (result.code === CODE.SUCCESS) {
      dispatch({
        type: get_task_detail,
        projectId,
        taskId,
        projectName: result.data.projsObject.name,
        taskName: result.data.taskName,
        deployStatus: result.data.status,
        testHost: result.data.testHost,
        prevHost: result.data.prevHost,
        serversList: result.data.list,
        markContent: result.data.mark,
        developer: result.data.fatDeveloper,
        admin: result.data.fatAdmin,
        tester: result.data.fatTester,
      });
      cb(result.data);
    }
  })
}